package www.wen.com.common.lib.util

import android.view.View
import androidx.core.view.isVisible
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.xuexiang.xupdate.entity.PromptEntity
import com.xuexiang.xupdate.entity.UpdateEntity
import com.xuexiang.xupdate.proxy.IUpdatePrompter
import com.xuexiang.xupdate.proxy.IUpdateProxy
import com.xuexiang.xupdate.service.OnFileDownloadListener
import com.xuexiang.xupdate.utils.UpdateUtils
import www.wen.com.common.R
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.view.popuview.AppUpdatePopupView
import java.io.File
import kotlin.math.roundToInt

/**
 * @author WEN
 * @Description:
 * @date 2021/9/30 2:36
 */
class CustomUpdatePrompter : IUpdatePrompter {

    private var mUpdateStatus = UpdateStatus.UPDATE_NOT

    override fun showPrompt(
            updateEntity: UpdateEntity,
            updateProxy: IUpdateProxy,
            promptEntity: PromptEntity,
    ) {

        val file = File(updateEntity.apkCacheDir)
        if(file.exists()){
            file.delete()
        }

        XPopup.Builder(updateProxy.context)
                .hasBlurBg(false)
                .isDestroyOnDismiss(true)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(
                        AppUpdatePopupView(
                                context = updateProxy.context!!,
                                isForceUpdate = updateEntity.isForce,
                                content = updateEntity.updateContent,
                                resultCallBack = { view: View, _: String, basePopupView: BasePopupView ->
                                    when (view.id) {
                                        R.id.tvNotUpDateApp -> {
                                            basePopupView.dismiss()
                                            UpdateUtils.saveIgnoreVersion(
                                                    updateProxy.context,
                                                    updateEntity.versionName
                                            )
                                        }
                                        R.id.tvUpDateApp -> {
                                            val appUpdatePopupView = basePopupView as AppUpdatePopupView
                                            updateProxy.startDownload(
                                                    updateEntity,
                                                    object : OnFileDownloadListener {
                                                        override fun onStart() {
                                                            mUpdateStatus = UpdateStatus.UPDATE_LOADING
                                                            appUpdatePopupView.numberProgressBar.isVisible = true
                                                            appUpdatePopupView.tvUpDateApp.isVisible = false
                                                            appUpdatePopupView.tvNotUpDateApp.isVisible = false
                                                        }

                                                        override fun onProgress(progress: Float, total: Long) {
                                                            appUpdatePopupView.numberProgressBar.progress = (progress * 100).roundToInt()
                                                        }

                                                        override fun onCompleted(file: File): Boolean {
                                                            mUpdateStatus = UpdateStatus.UPDATE_COMPLETED
                                                            basePopupView.dismiss()
                                                            toast("更新完毕")
                                                            return true
                                                        }

                                                        override fun onError(throwable: Throwable) {
                                                            mUpdateStatus = UpdateStatus.UPDATE_ERROR
                                                            basePopupView.dismiss()
                                                            toast("更新错误,请尝试通过官网下载最新版本")
                                                        }
                                                    })
                                        }
                                    }
                                }
                        )
                )
                .show()
    }

    enum class UpdateStatus{

        UPDATE_LOADING,UPDATE_COMPLETED,UPDATE_ERROR,UPDATE_NOT

    }

}